/**
 * Created by no1 on 2015/6/27.
 */
'use strict';
var mysql = require('./mysql.js');
var C = require('../config/index');
var F = require('../common/function');
var o2oF = require('../common/o2ofunction');
var _ = require('underscore');
var _s = require('underscore.string');
var md5 = require('md5');
_.v = require('validator');
var _o2oServiceApi = require('../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;


/*导入商圈*/
exports.importDistrict = function*(Env) {
  let businessData = yield mysql.query('select * from mh_business_region');
  for (let i in businessData) {
    var bodyData = {
      "name": businessData[i].region_name,
      "code": businessData[i].region_id,
      "pCode": businessData[i].parent_id,
      "level": businessData[i].region_type,
      "loc": businessData[i].center_location.split(',')
    };
    let o2o = new o2oServiceAPI();
    let result = yield o2o.post('district', 'add', 1, bodyData);
    console.log(result);
  }
};

/*导入店面*/
exports.importShop = function*() {
  let storeData = yield mysql.query('select photoSrc,name,address,tel,lng,lat,regions,otherInfo,otherInfo1 from mh_dianPingStore where not ISNULL(lat) and not ISNULL(lng) ');
  for (let i in storeData) {
    let arrTel = [];
    if (storeData[i]['tel'].replace(' ', '') == '') {
      arrTel.push(storeData[i]['tel']);
    } else if (storeData[i]['tel'].indexOf(',') < 1) {
      arrTel.push(storeData[i]['tel']);
    } else if (storeData[i]['tel'].indexOf(',') > -1) {
      arrTel = storeData[i]['tel'].split(',');
    }
    let defaultCategory = {};
    defaultCategory.name = "美容";
    defaultCategory.code = 1;
    defaultCategory.pCode = 0;
    defaultCategory.level = 1;
    defaultCategory.type = 2;
    defaultCategory.status = 1;
    if (storeData[i].otherInfo == '') {
      storeData[i].otherInfo = '营业时间 9:00~21:00(模拟数据)';
    }
    var bodyData = {
      "name": storeData[i].name,
      "act": 1,
      "image": storeData[i].photoSrc,
      "tel": arrTel,
      "address": storeData[i].address,
      "status": 1,
      "desc": storeData[i].otherInfo,
      "loc": [parseFloat(storeData[i].lng), parseFloat(storeData[i].lat)],
      "district": JSON.parse(storeData[i].regions),
      "other_info": storeData[i].otherInfo1,
      "categories": [defaultCategory]
    };
    //console.log(bodyData);
    let result = yield o2oServiceApi.post('shop', 'add', 1, bodyData);
    console.log(result);
  }
};

/*
 @todo API01 获取地区
 type (string, optional): tree整棵树=default, single单个节点, directChildren直接child
 string
 Enum:	"tree", "single", "directChildren", "allChildren"
 */
exports.district = function*(Env) {
  let fields = Env.I;
  let body = {};
  body.act = 1;
  if (!parseInt(fields.regionid) == 0) {
    body.code = fields.regionid || 440100;
  } else {
    body.code = 0;
  }
  switch (parseInt(fields.act)) {
    case 1://根据传入id获取下一级所有的地区列表
      body.type = 'directChildren';
      break;
    case 2://根据传入id取所有子级地区列表
      body.type = 'allChildren';
      break;
    case 3://根据id获取地区信息
      body.type = 'single';
      break;
  }
  var ret = yield o2oServiceApi.post('district', 'info', 1, body);
  return F.returnMsg(200, 'OK', 3, ret['result']);
};

/*
 @todo API03 获取城市
 pCode (integer, optional): 不填代表查询全国城市 ,
 act (integer, optional): 功能项目 必填,现在只能为1
 */
exports.city = function*(Env) {
  let fields = Env.I;
  let body = {};
  body.act = 1;
  body.pCode = fields.pCode || '';
  if (body.pCode == '') {
    delete body.pCode;
  }
  var ret = yield o2oServiceApi.post('district', 'city', 1, body);
  return F.returnMsg(200, 'OK', 3, ret['result']);
};

/*
 @todo API04 首页list
 type 10:返回商家
 type 11：返回优惠信息
 */
exports.itemList = function*(Env) {
  let fields = Env.I;
  //act=1 获得商家列表；act=2获得商品（优惠）列表
  if (fields.category == '' || fields.category === 'undefined') {
    return F.returnMsg(400, '请输入类别', 1);
  }
  if ((!_.v.isIn(parseInt(fields.sort), [1, 2, 3])) && parseInt(fields.act) == 1) {
    return F.returnMsg(400, '请输入正确的排序类型', 1);
  } else if (!_.v.isInt(parseInt(fields.sort)) && parseInt(fields.act) == 1) {
    return F.returnMsg(400, '排序类型输入有误.', 1);
  }
  if (fields.lng == '' || fields.lng === 'undefined') {
    fields.lng = '0';
  }
  if (fields.lat == '' || fields.lat === 'undefined') {
    fields.lat = '0';
  }
  if ((!_.v.isFloat(fields.lng) && !_.v.isInt(fields.lng)) || (!_.v.isFloat(fields.lat) && !_.v.isInt(fields.lat))) {
    return F.returnMsg(400, '经纬度格式有误.', 1);
  }
  let body = {};
  if (fields.radius != undefined && fields.radius != '') {
    if (!_.v.isInt(fields.radius)) {
      return F.returnMsg(400, '搜索半径输入类型有误.', 1);
    }
    body.radius = fields.radius;
  }
  body.act = 1;
  body.page = fields.page || 1;
  body.pageSize = fields.pageSize != undefined ? parseInt(fields.pageSize) : C.pageSize;
  body.sort = 'grade';
  switch (fields.sort) {//1:grade;2:near;3:smart
    case 2:
      body.sort = 'near';
      break;
    case 3:
      body.sort = 'smart';
      break;
  }
  body.loc = [];
  body.loc.push(parseFloat(fields.lng));
  body.loc.push(parseFloat(fields.lat));
  body.districtCode = parseInt(fields.districtCode) || 0;
  if (body.districtCode == 0) {
    delete  body.districtCode;
  }
  let retList = {};
  retList.pageIndex = body.page;

  if (fields.act == 1) {//获取商家列表
    body.cateCodes = parseInt(fields.category);
    var ret = yield o2oServiceApi.post('shop', 'list', 1, body);
    for (let i in ret['result']) {
      ret['result'][i] = yield o2oF.o2oProcessShopItem(ret['result'][i]);
    }
  } else if (fields.act == 2) {//获取商品、优惠列表
    let wareQuery = {};
    wareQuery.page = body.page;
    wareQuery.pageSize = body.pageSize;
    wareQuery.districtCode = body.districtCode;
    wareQuery.cateCode = parseInt(fields.category);
    wareQuery.loc = body.loc;
    wareQuery.act = 1;
    var ret = yield o2oServiceApi.post('ware', 'list', 1, wareQuery);
    for (let i in ret['result']) {
      ret['result'][i] = yield o2oF.o2oProcessWareItem(ret['result'][i]);
    }
  }
  if (parseInt(ret['result']['error']) == 1 || ret.status == 400||ret['result'].length==0) {
    return F.returnMsg(300, '已经没有数据了', 1);
  }
  retList.items = ret['result'];
  return F.returnMsg(200, 'OK', 3, retList);
};

/*
 @todo API03 获取单个店面或者单个商品
 act=1获取单个店面；
 act=2获取单个商品（详情页使用）；
 act=3获取单个商品的名称，单价信息
 */
exports.getSingleItem = function*(Env) {
  let fields = Env.I;
  let page = parseInt(fields.page) || 0;
  if (page > 1) {
    return F.returnMsg(300, '没有有效的数据.', 1);
  }
  let body = {};
  body.act = 1;
  switch (parseInt(fields.act)) {//1:获取店面信息；2：获得商品详细信息（for H5页面）;3：获取商品名称跟价格（for 确定订单页面）
    case 1://查询单个店面 Env.request.body.fields
      body.id = parseInt(fields.shopid) || 0;
      if (body.id == 0) {
        return F.returnMsg(400, '请先输入shopid.', 1);
      }
      var ret = yield o2oServiceApi.post('shop', 'detail', 1, body);
      if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
        return F.returnMsg(300, '没有有效的数据.', 1);
      }
      ret['result'] = yield o2oF.o2oProcessShopItem(ret['result']);
      //get the ware list under the certain shop
      var warelist = yield o2oServiceApi.post('ware', 'list', 1, {act: 2, sid: body.id});
      for (let i in warelist['result']) {
        warelist['result'][i] = yield o2oF.o2oTranslateProperty(warelist['result'][i]);
      }
      ret['result'].warelist = warelist['result'];
      var subShoplist = yield  o2oServiceApi.post('shop', 'list', 1, {
        act: 3,
        vendorid: ret['result'].vendorid
      });
      if (subShoplist && subShoplist['result'].length > 0) {
        ret['result'].subShopCount = subShoplist['result'].length;
      } else
        ret['result'].subShopCount = 0;
      ret['result'].type = 12;//type 12是商家详情
      ret['result'].model = 'shopinfo';// 商品详情

      break;
    case 2:
    case 3://查询单个商品
      body.id = parseInt(fields.wareid) || 0;
      if (body.id == 0) {
        return F.returnMsg(400, '请先输入wareid.', 1);
      }
      if (fields.act == 3) {//查询商品名称，单价（确认订单时用）
        body.act = 2;
        var ret = yield o2oServiceApi.post('ware', 'detail', 1, body);
        if (ret.status == 200) {
          //ret['result'].wareid = ret['result'].id;
          //delete ret['result'].id;
          return F.returnMsg(200, 'OK', 3, ret['result']);
        }
        return F.returnMsg(300, '无数据', 3);
      }
      var lng = parseFloat(fields.lng) || 0;
      var lat = parseFloat(fields.lat) || 0;
      if (lng > 0 && lat > 0) {
        body.loc = [lng, lat];
      }
      var ret = yield o2oServiceApi.post('ware', 'detail', 1, body);
      if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
        return F.returnMsg(300, '没有有效的数据.', 1);
      }
      ret['result'].subShopCount = ret['result']['shops'].length || 0;
      ret['result'] = yield o2oF.o2oProcessWareItem(ret['result']);
      ret['result'].btn = {status: 1, title: '立即抢购', btn_act: 1};
      //获取商品的店面信息
      var shopData = yield o2oServiceApi.post('shop', 'detail', 1, {act: 1, id: ret['result'].shopid});
      if (!(parseInt(shopData['result']['error']) == 1 || shopData.status == 400)) {
        shopData['result'] = yield o2oF.o2oProcessShopItem(shopData['result']);

        ret['result'].address = shopData['result'].address;
        ret['result'].tel = shopData['result'].tel;
        ret['result'].shopGrade = shopData['result'].grade;
        //let subShoplist = yield  o2oServiceApi.post('shop', 'list', 1, {
        //  act: 3,
        //  vendorid: shopData['result'].vendorid
        //});
      }
      ret['result'].type = 13;//type 13是项目详情
      ret['result'].model = 'wareinfo';// 商品详情
      //get APP config
      var dbConfigData = yield o2oServiceApi.post('appConfig', 'detail', 1, {act: 1});
      var modelEntity = _.filter(dbConfigData['result']['H5_model'], function (entity) {
        return entity.model == ret['result'].model;
      });
      ret['result'].mcode = modelEntity[0].mcode;
      break;
  }
  return F.returnMsg(200, 'OK', 3, {items: [ret['result']]});
};

/*
 @todo API06 获取类别
 pCode (integer, optional): 父级类别，不传或传0为取得所有父类 ,
 act (integer, optional): 功能项目 必填,现在只能为1
 */
exports.getCategory = function*(Env) {
  let fields = Env.I;
  let body = {};
  body.act = 1;
  body.pCode = fields.pCode || 0;
  if(fields.type && _.v.isInt(fields.type)){
    body.type=fields.type;
  }
  //if (body.pCode == '') {
  //    delete body.pCode;
  //}
  var ret = yield o2oServiceApi.post('category', 'list', 1, body);
  if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
    return F.returnMsg(300, '没有有效的数据.', 1);
  }
  return F.returnMsg(200, 'OK', 3, ret['result']);
};

/*
 @todo API07_1 获取分店列表
 shopid 要查找分店的shopid
 vendorid 商家id
 act (integer, optional): 功能项目 必填,现在只能为1
 */
exports.getSubShop = function*(Env) {
  let fields = Env.I;
  let body = {};
  body.act = 1;
  //body.shopid = fields.shopid || 0;
  body.vendorid = fields.vendorid || 0;
  if (body.vendorid == 0) {
    return F.returnMsg(400, '店面Id或者商家Id输入有误..', 1);
  }
  var ret = yield  o2oServiceApi.post('shop', 'list', 1, {
    act: 3,
    vendorid: body.vendorid
  });
  if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
    return F.returnMsg(300, '没有有效的数据.', 1);
  } else {
    for (let i in ret['result']) {
      ret['result'][i] = yield o2oF.o2oProcessShopItem(ret['result'][i]);
    }
  }
  return F.returnMsg(200, 'OK', 3, {items: ret['result']});
};

/*
 @todo API07_2 获取项目适用分店列表
 根据传入id列表查询
 act (integer, optional): 功能项目 必填,现在只能为1
 shopid:店面Id
 wareid:商品id
 */
exports.getWareShop = function*(Env) {
  let fields = Env.I;
  let wareid = fields.wareid || 0;

  if (wareid == 0) {
    return F.returnMsg(400, '商品Id输入有误..', 1);
  } else {
    if (!_.v.isInt(wareid)) {
      return F.returnMsg(400, '商品Id输入有误..', 1);
    }
  }
  let wareDetailData = yield o2oServiceApi.post('ware', 'detail', 1, {act: 1, id: parseInt(wareid)});
  if (parseInt(wareDetailData['result']['error']) == 1 || wareDetailData.status == 400) {
    return F.returnMsg(300, '找不到对应的商品.', 1);
  } else {
    let shopidArr = [];
    let shopArr = wareDetailData['result'].shops;
    for (let shop of shopArr) {
      shopidArr.push(shop.sid);
    }
    if (shopidArr.length == 0) {
      return F.returnMsg(300, '已经没有数据了.', 1);
    }
    var subShopData = yield o2oServiceApi.post('shop', 'list', 1, {act: 2, ids: shopidArr});
    if (parseInt(subShopData['result']['error']) == 1 || subShopData.status == 400) {
      return F.returnMsg(300, '已经没有数据了.', 1);
    }

    for (let i in subShopData['result']) {
      subShopData['result'][i] = yield o2oF.o2oProcessShopItem(subShopData['result'][i]);
    }
  }
  return F.returnMsg(200, 'OK', 3, {items: subShopData['result']});
};

/*
 @todo API08 订单相关接口

 act (integer, optional): 功能项目1：提交订单；2：取消订单；3：支付订单；4：查询当前用户所有订单，5：查询具体订单
 */
exports.orderOperation = function*(Env) {
  let fields = Env.I;
  let loginStatus = fields.loginStatus;
  if (!loginStatus.status) {
    return F.returnMsg(401, '请先登录', 3);
  }
  let orderid = parseInt(fields.orderid) || 0;
  let wareid = fields.wareid || 0;
  let quantity = fields.quantity || 0;
  let money = fields.money || 0;
  let payWay = fields.payWay || -1;
  let act=parseInt(fields.act);
  let body = {};
  body.act = 1;
  if (act == 1) {//提交订单
    if (quantity == 0) {
      return F.returnMsg(400, '请传入有效的数量.', 1);
    } else if (!_.v.isInt(quantity)) {
      return F.returnMsg(400, '订单格式输入有误，请检查.', 1);
    } else {
      quantity = parseInt(quantity);
    }

    if (money == 0) {
      return F.returnMsg(400, '请传入金额.', 1);
    } else if (!_.v.isFloat(money)) {
      return F.returnMsg(400, '金额格式输入有误，请检查.', 1);
    } else {
      money = parseFloat(money);
    }

    if (wareid == 0) {
      return F.returnMsg(400, '请传入有效的商品id.', 1);
    } else if (!_.v.isInt(wareid)) {
      return F.returnMsg(400, '商品id格式输入有误，请检查.', 1);
    }
    else {
      wareid = parseInt(wareid);
      var ret = yield o2oServiceApi.post('ware', 'detail', 1, {act: 1, id: wareid});
      if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
        return F.returnMsg(300, '项目id在数据库中不存在.', 1);
      }else{
        var shoplist=[];
        let shopidArr = [];
        let shopArr = ret['result'].shops;
        for (let shop of shopArr) {
          shopidArr.push(shop.sid);
        }
        var subShopData = yield o2oServiceApi.post('shop', 'list', 1, {act: 2, ids: shopidArr});
        for (let i in subShopData['result']) {
          subShopData['result'][i] = yield o2oF.o2oProcessShopItem(subShopData['result'][i]);
          shoplist.push(subShopData['result'][i]);
        }
      }
    }
    let orderBody = {
      act: 1,
      wareid: wareid,
      uid: loginStatus.uid,
      price: money,
      quantity: quantity,
      total_price: money
    };
    var ret = yield  o2oServiceApi.post('order', 'add', 1, orderBody);
    if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
      return F.returnMsg(300, ret['result']['msg'], 1);
    } else {
      ret['result']['wareName'] = '[' + ret['result']['vendorName'] + ']' + ret['result']['wareName'];
      ret['result'].shoplist = shoplist;
      delete ret['result'].vendorName;
      return F.returnMsg(200, '订单保存成功.', 1, ret['result']);
    }
  } else if (act == 2 || act == 3) {//取消(2)、支付(3)订单
    if (orderid == 0) {
      return F.returnMsg(400, '请先传入有效的订单编号.', 1);
    }
    var orderOperationArray=yield o2oF.o2oGetOrderOperation(orderid);
    let orderBody = {act: 1, orderid: orderid};
    let tipsMsg;
    var ret = yield  o2oServiceApi.post('order', 'detail', 1, orderBody);
    if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
      return F.returnMsg(300, '系统未能找到传入的订单', 1);
    } else {
      if (parseInt(ret['result']['uid']) != loginStatus.uid) {
        return F.returnMsg(300, '权限验证失败，不能修改他人订单', 1);
      }
      if (act == 2) { // 取消订单
        if(!_.v.isIn(1,orderOperationArray)){//验证当前订单能否被取消(operation:1)
          return F.returnMsg(300, '当前订单不能被取消，请联系在线客服人员', 1);
        }
        orderBody.updateInfo = {status: 2};//status 2:取消
        tipsMsg = '取消订单成功';
      } else {//支付订单
        if (payWay == -1) {
          return F.returnMsg(400, '请先传入付款方式.', 1);
        }
        if(!_.v.isIn(2,orderOperationArray)){//验证当前订单能否被支付(operation:2)
          return F.returnMsg(300, '该订单已被付款或已取消', 1);
        }
        orderBody.updateInfo = {pay_way: payWay};
        tipsMsg = '订单支付成功';
      }
      var result = yield  o2oServiceApi.post('order', 'update', 1, orderBody);
      if (parseInt(result['result']['error']) == 1 || result.status == 400) {
        return F.returnMsg(300, result['result']['msg'], 1);
      } else {
        return F.returnMsg(200, tipsMsg, 1, result['result'])
      }
    }
  } else if (act == 4) {//查询用户全部订单
    var ret = yield  o2oServiceApi.post('order', 'list', 1, {act: 1, uid: loginStatus.uid});
    if (parseInt(ret['result']['error']) == 1 || ret.status == 400||ret['result'].length==0) {
      return F.returnMsg(300, '没有有效的数据', 1);
    } else {
      for(let order of ret['result']){
        order.operations=yield o2oF.o2oGetOrderOperation(order.orderid,order);
        order.cid=20;//@todo write a function to get thread category based on ware.category
        order.threadCategory='护肤';
      }
      return F.returnMsg(200, '成功.', 3, ret['result']);
    }
  } else if (act == 5) {//查询具体一个订单
    if (orderid == 0) {
      return F.returnMsg(400, '请先传入订单编号.', 1);
    }
    try {
      let body = {act: 1, orderid: orderid};
      var lng = parseFloat(fields.lng) || 0;
      var lat = parseFloat(fields.lat) || 0;
      if (lng > 0 && lat > 0) {
        body.loc = [lng, lat];
      }
      var ret = yield  o2oF.o2oGetOrderDetailByOrderId(body);
    }
    catch (e) {
      return F.returnMsg(300, '系统未能找到传入的订单', 1);
    }
    if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
      return F.returnMsg(300, '系统未能找到传入的订单', 1);
    } else {
      if (loginStatus.uid != parseInt(ret['result']['uid'])) {
        return F.returnMsg(300, '权限不足，未能查看他人订单.', 1);
      }
      if (ret['result']['loc'] && ret['result']['loc'].length >= 2) {
        ret['result'].lng = ret['result']['loc'][0];
        ret['result'].lat = ret['result']['loc'][1];
        delete ret['result'].loc;
      }
      for (let shop of ret['result']['ShopInfo']) {
        if (shop['loc'] && shop['loc'].length >= 2) {
          shop.lng = shop['loc'][0];
          shop.lat = shop['loc'][1];
          delete shop.loc;
          shop.shopid = shop.id;
          delete shop.id;
        }
        shop.tel = yield o2oF.o2oShopTelephonesSort(shop['tel']);
      }
      let userData = yield F.userMobile(loginStatus.uid);
      if(userData.length>0){
        ret['result']['mobile'] = userData[0].mobile;}
      else{
        ret['result']['mobile']='';
      }
      ret['result']['operations']=yield o2oF.o2oGetOrderOperation(ret['result'].orderid,ret['result']);
      return F.returnMsg(200, '成功.', 3, ret['result']);
    }
  }
};

/*
 @todo API09 APP端添加未认证店面
 act (integer, optional): 功能项目2
 */
exports.addUnautherizedShop=function*(Env){
  let fields = Env.I;
  let loginStatus = fields.loginStatus;
  if (!loginStatus.status) {
    return F.returnMsg(401, '请先登录', 3);
  }
  let name=fields.name;
  let lastCode=fields.lastCode;
  let shopInfo={name:name,lastCode:lastCode,act:2};
  let ret=yield o2oF.o2oSaveShopInfo(shopInfo);
  if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
    return F.returnMsg(400, '保存失败,数据库已存在同名机构', 1);
  }else{
    let shopResult = _.pick(ret['result'], 'id', 'name');
    shopResult.organizationID = shopResult.id;
    return F.returnMsg(200, '保存成功', 1,shopResult);
  }
};


/*
 @todo API10 APP端模糊查询商店接口
 act (integer, optional): 4：模糊查询商店接口
 */
exports.searchShopByName=function*(Env){
  let fields = Env.I;
  let act=fields.act;
  if(act==1){//模糊查找商店
    let name=fields.name;
    if(_.v.trim(name)==''){
      return F.returnMsg(400, '请输入商店名称', 1);
    }
    let page=fields.page||1;
    let pageSize = fields.pageSize || 50;
    let shopInfo={name:name,act:4,page:parseInt(page),pageSize:parseInt(pageSize)};
    let ret = yield  o2oServiceApi.post('shop', 'list', 1, shopInfo);
    if (parseInt(ret['result']['error']) == 1 || ret.status == 400) {
      return F.returnMsg(400, '已经没有数据了', 1);
    }else{
      for(let shop of ret['result']){
        shop.organizationID = shop.id;
      }
      return F.returnMsg(200, '成功', 3,ret['result']);
    }
  }else if(act==2){//返回热门机构，虚拟数据，只在内网返回
    if (C.env != 'production') {
      return F.returnMsg(200, '成功', 3,[{
        "name": "(搜狐)美业",
        "id": 1404,
        organizationID: 1404

      }]);
    }else{
      return F.returnMsg(200, '成功', 3,[{}]);
    }
  }
};

/*
 @todo API11 APP端获取Mongo省市列表
 act (integer, optional): 4：模糊查询商店接口
 */
exports.getDistrict=function*(Env){
  let fields = Env.I;
  let pCode=fields.pCode;
  let act=fields.act;
  let body={act:1};
  if(act==2){//获取市级列表
    body.pCode=pCode;
  }
  let result={};
  if(act==1){
    result=yield o2oServiceApi.post('district', 'province', 1, body);
  }else if(act==2){
    result=yield o2oServiceApi.post('district', 'list', 1, body);
  }
  if (parseInt(result['result']['error']) == 1 || result.status == 400) {
    return F.returnMsg(400, '已经没有数据了', 1);
  }else{
    for(let record of result['result']){
      delete record.loc;
      delete record.updateAt;
      delete record._id;
    }
    return F.returnMsg(200, '成功', 3,result['result']);
  }
};

exports.articleTest = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'test', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};

exports.articleCleanReq = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'cleanreq', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleLogFlow = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'logflow', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleLogFlow2 = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'logflow2', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};

exports.articleAddBatch = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'addBatch', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleTermfreq = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'termfreq', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleReBuild = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'reBuild', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleIndex = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'index', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleQSearch = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'qsearch', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleCScore = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'cscore', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleAutocate = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'autocate', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleSimilar = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'similar', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleSimilar2 = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'similar2', 1, env.I);
  return result;
};
exports.articleTermvector = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'termvector', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};
exports.articleAnalyze = function*(env) {
  let result = yield  o2oServiceApi.post('article', 'analyze', 1, env.I);
  return F.returnMsg(200, 'OK', 3, result);
};